//
// Picture.java
// ImageGenius
//
// Created by Zhang, Yifan on Jan 10, 2013
// Copyright 2013 Ming Microsystems. All rights reserved.
//

package com.mms.imagegenius.datastructure;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.shapes.Shape;

public class MMSPicture {
	
	/**
	 * TODO
     * Picture should have a constructor as file source passed in the
	 * future a full resolution Bitmap created may cause Out of Memory
	 * Error
	 */
	private Bitmap bitmap;
	private Matrix transform;
	private PictureType pictureType;
	private Shape shape;
	public static final String BUNDLEKEY_STRING = "MMSPicture";

	public enum PictureType {
		PictureTypeForeground, PictureTypeBackground,
	};

	// Constructors
	public MMSPicture(Bitmap bitmap) {
		this(bitmap, new Matrix());
	}

	public MMSPicture(Bitmap bitmap, Matrix transform) {
		this.bitmap = bitmap;
		this.transform = transform;
	}

	public Bitmap getScaledBitmap(int width, int height){
		return Bitmap.createScaledBitmap(this.bitmap, width, height, false);
	}
	
	/**
	 * @return the bitmap
	 */
	public Bitmap getBitmap() {
		return bitmap;
	}

	/**
	 * @param bitmap the bitmap to set
	 */
	public void setBitmap(Bitmap bitmap) {
		this.bitmap = bitmap;
	}

	/**
	 * @return the transform
	 */
	public Matrix getTransform() {
		return transform;
	}

	/**
	 * @param transform the transform to set
	 */
	public void setTransform(Matrix transform) {
		this.transform = transform;
	}

	/**
	 * @return the pictureType
	 */
	public PictureType getPictureType() {
		return pictureType;
	}
	
	/**
	 * @return the shape of this picture
	 */
	Shape getShape() {
		return shape;
	}

	/**
	 * @param shape the shape to set
	 */
	void setShape(Shape shape) {
		this.shape = shape;
	}

	// Convenience wrappers
	public void rotate(float degrees) {
		transform.postRotate(degrees);
	}

	public void translate(float dx, float dy) {
		transform.postTranslate(dx, dy);
	}

	public Bitmap getTransformedBitmap() {
		return Bitmap.createBitmap(this.bitmap, 0, 0, this.bitmap.getWidth(), this.bitmap.getHeight(), this.transform, false);
	}
	
	public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
		// Raw height and width of image
		final int height = options.outHeight;
		final int width = options.outWidth;
		int inSampleSize = 1;

		if (height > reqHeight || width > reqWidth) {
			if (width > height) {
				inSampleSize = Math.round((float) height / (float) reqHeight);
			} else {
				inSampleSize = Math.round((float) width / (float) reqWidth);
			}
		}
		return inSampleSize;
	}

	public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {

		// First decode with inJustDecodeBounds=true to check dimensions
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeResource(res, resId, options);

		// Calculate inSampleSize
		options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

		// Decode bitmap with inSampleSize set
		options.inJustDecodeBounds = false;
		return BitmapFactory.decodeResource(res, resId, options);
	}

}
